function formatNumberToThousands(num: number): string {
  if (isNaN(num) || typeof num !== 'number') {
    throw new TypeError('必须是一个有效数字');
  }

  const [integerPart, decimalPart] = num.toString().split('.');

  let formatterInteger = ''; //123456
  for (let i = integerPart.length - 1; i >= 0; i--) {
    formatterInteger = integerPart[i] + formatterInteger;
    if ((integerPart.length - i) % 3 === 0 && i !== 0) {
      formatterInteger = ',' + formatterInteger;
    }
  }

  formatterInteger = decimalPart
    ? formatterInteger + '.' + decimalPart
    : formatterInteger;
  return formatterInteger;
}

export default formatNumberToThousands;
